package com.et.u2

import android.os.Bundle
import android.view.LayoutInflater
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import com.et.core.init
import com.et.core.out.out.OutBroadcast
import com.et.core.out.out.OutHelper
import com.et.core.release
import com.et.u2.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private val outHelperCall = {
        OutHelper.binderService(applicationContext)
    }

    private lateinit var binding: ActivityMainBinding
    private lateinit var mainModel: MainModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        init(applicationContext)
        ActivityMainBinding.inflate(LayoutInflater.from(this)).also {
            binding = it
            setContentView(it.root)
        }
        OutHelper.addDeathCallBack(outHelperCall)
        initData()
        initEvent()

//        Timer().schedule(object : TimerTask() {
//            override fun run() {
//                Log.d(TAGS.vending, "testInsertsGoods")
//                val list = mutableListOf<ChannelEntity>()
//                for (i in 1..48) {
//                    list.add(ChannelEntity.create(11, i).apply {
//                        status = Status.ENABLED
//                        priceCent = i
//                    })
//                }
//                GoodsHelper.getInstance().adds(list)
//            }
//        }, 5_000)
    }

    private fun initData() {
        mainModel =
            ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory(application)).get(
                MainModel::class.java
            )
    }

    private fun initEvent() {

        OutBroadcast.addCallBack {
            Toast.makeText(applicationContext, "OutBroadcast", Toast.LENGTH_SHORT).show()
        }
        mainModel.initGoodsHelper(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        release(applicationContext)
    }

}